package com.zks.example;

import org.junit.Test;

import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.stream.IntStream;

public class Te {
    static AtomicStampedReference<Integer> money = new AtomicStampedReference<>(19, 0);

    @Test
    public void te() {
        int stamp = money.getStamp();
        Integer reference = money.getReference();
        boolean b = money.compareAndSet(reference, reference + 1, stamp, stamp + 1);
        System.out.println(b);

    }

    @Test
    public void t() {
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        concurrentHashMap.computeIfAbsent("key", key -> "b");
        concurrentHashMap.computeIfAbsent("key", key -> "c");
        System.out.println(concurrentHashMap.get("key"));

        concurrentHashMap.computeIfPresent("key", (key, value) -> "c");
        System.out.println(concurrentHashMap.get("key"));
        //concurrentHashMap.reduce()

    }

    @Test
    public void ts() {
        ConcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashMap();
        for (int i = 1; i < 3; i++) {
            concurrentHashMap.put(String.valueOf(i) + "s ", i);
        }
        Integer integer = concurrentHashMap.reduceValues(2, (i, j) -> j + j);
        System.out.println(integer);


        String s = concurrentHashMap.reduceKeys(2, (i, j) -> i + j);
        System.out.println(s);

        String reduce = concurrentHashMap.reduce(2, (k, v) -> k + v, (j, k) -> j + "*" + k + "   ");
        System.out.println(reduce);
        //找到第一个使function 不为null的只
        String search = concurrentHashMap.search(2, (k, v) -> {
            if (v == 2) {
                return k;
            }
            return null;

        });

        System.out.println(search);
    }

    @Test
    public void skipListMapTest() {
        ConcurrentSkipListMap<Integer, Integer> cslMap = new ConcurrentSkipListMap<Integer, Integer>();
        cslMap.put(1, 1);
        cslMap.put(2, 2);
        cslMap.put(4, 1);
        cslMap.put(5, 2);
        cslMap.put(3, 1);
        cslMap.put(6, 2);
        cslMap.put(7, 1);
        cslMap.put(8, 2);
        cslMap.put(9, 2);
        cslMap.put(10, 2);
        cslMap.put(11, 2);
        cslMap.put(13, 34);
        cslMap.put(14, 34);
        cslMap.put(14, 35);
        Integer startKey = 2;
        Integer endKey = 3;
        ConcurrentNavigableMap<Integer, Integer> cs = cslMap.subMap(startKey, endKey);
        ConcurrentNavigableMap<Integer, Integer> c = cslMap.headMap(3);
        ConcurrentNavigableMap<Integer, Integer> css = cslMap.tailMap(3);

        Map.Entry<Integer, Integer> integerIntegerEntry = cslMap.ceilingEntry(1);
        cslMap.compute(2, (key, v) -> {
            return v + 10;
        });

        //小于等于key 的最大值
        Integer integer = cslMap.floorKey(13);
        //小于 key的最大值
        Integer integer2 = cslMap.lowerKey(13);

        //大于key的最小值
        Integer integer1 = cslMap.higherKey(12);
        //大于等于key的最小值
        Integer integer3 = cslMap.ceilingKey(12);

        //返回key 降序
        NavigableSet<Integer> integers = cslMap.descendingKeySet();

        //如果给定的key 不存在，则直接加入，否则，后面的函数 v1为之前的value v2 为现在传进去的value
        Integer merge = cslMap.merge(14, 35, (v1, v2) -> {
            return v1 + v2;
        });
        System.out.println(cs);

    }

    @Test
    public void treeMapTest(){
        TreeMap<Integer,Integer> treeMap=new TreeMap<>();
        treeMap.put(1, 1);
        treeMap.put(2, 2);
        treeMap.put(4, 1);
        treeMap.put(5, 2);
        treeMap.put(3, 1);
        treeMap.put(6, 2);
        treeMap.put(7, 1);
        treeMap.put(8, 2);
        treeMap.put(9, 2);
        treeMap.put(10, 2);
        treeMap.put(11, 2);
        treeMap.put(13, 34);
        treeMap.put(14, 34);
        treeMap.put(14, 35);


        System.out.println(treeMap);

    }
}
